/**
 * @author yxw
 * date : 2013年10月3日 下午8:51:09 
 */
package com.tzinfo.tzt.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.activity.util.ConvertSwitch;
import com.tzinfo.tzt.activity.util.MyApplication;
import com.tzinfo.tzt.activity.view.ImagePagerAdapter;
import com.tzinfo.tzt.activity.view.MyProgressDialog;
import com.tzinfo.tzt.activity.view.VerticalScrollView;
import com.tzinfo.tzt.config.AppDataConfig;
import com.tzinfo.tzt.domain.MyResultList;
import com.tzinfo.tzt.domain.NewhouseBuyInfo;
import com.tzinfo.tzt.domain.NewhouseDetail;
import com.tzinfo.tzt.domain.NewhouseDetailHousePic;
import com.tzinfo.tzt.domain.NewhouseList;
import com.tzinfo.tzt.domain.NewhouseNewsDetail;
import com.tzinfo.tzt.util.HttpTools;
import com.tzinfo.tzt.util.JsonTools;
import com.tzinfo.tzt.util.ValidityTools;
import com.umeng.socialize.controller.UMWXHandler;
import com.umeng.socialize.media.UMImage;

public class NewHouseDetailActivity extends BaseActivity implements
		OnClickListener, OnItemClickListener {

	private List<Map<String, Object>> imgData, housePicData, newsData;
	private Dialog dialog;
	private MyResultList newHouseDetailResult, newhouseBuySignResult,
			newHouseBuyInfoResult;
	private Intent thisIntent;
	private String sHouseId = "";
	private int attrType = 0;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options;
	private boolean isMoreNews = false, isNoNews = false;
	private NewhouseDetail newhouseDetail;
	private String tuanPhone = "";
	private NewhouseBuyInfo newhouseBuyInfo;//团购的信息
	private NewhouseList newhouseList;
	private String tuanJsonString;

	// 标志
	private boolean collectionChecked = false;

	// UI组件
	private Button backBtn, callBtn, getPreBtn, collectionBtn, shareBtn;
	private VerticalScrollView mScrollView;
	private TextView text_type_tuan,text_tuangou_endtime;
	private TextView[] houseBuyView = new TextView[4];
	private TextView[] textViews = new TextView[11];
	private ViewPager imagesPager, roomTypePager;
	private ImagePagerAdapter imagePagerAdapter;
	private ImageClickPagerAdapter housePicPagerAdapter2;
	private ImageView mapView, tuanView, noPicView;
	private SimpleAdapter newsSimpleAdapter;
	private ListView newsListView;
	private EditText phoneNumEidtText;
	private LinearLayout tuangouLayout, tuanLayoutBtn;
	private RelativeLayout isMoreNewsLayout, isNoNewsLayout, moreParasLayout;
	private RelativeLayout mapLayout;
	
	private Drawable drawableSelected;
	private Drawable drawableNormal;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_newhouse_detail);
		thisIntent = this.getIntent();
		newhouseList = (NewhouseList) thisIntent
				.getSerializableExtra("newhouseList");
		String strPara = thisIntent.getStringExtra("houseId");
		String[] strParas = strPara.split("&&");
		sHouseId = strParas[0];
		if (strParas.length >= 2) {
			attrType = Integer.parseInt(strParas[1]);
		}
		setShareContent();
		init();
	}

	private void setShareContent() {
		String shareContent = String.format(
				this.getResources().getString(R.string.share_content),
				newhouseList.getName(), newhouseList.getLogo());
		// 设置分享内容
		mController.setShareContent(shareContent);
		// 设置分享图片, 参数2为图片的地址
		mController.setShareMedia(new UMImage(this, newhouseList.getLogo()));

		// 微信图文分享必须设置一个url
		String contentUrl = newhouseList.getLogo();

		// 添加微信朋友圈平台
		mController.getConfig().supportWXCirclePlatform(this,
				AppDataConfig.WXapp_id, contentUrl);
		mController.getConfig().supportWXPlatform(this, AppDataConfig.WXapp_id,
				contentUrl);
	}
	private void changeShareUrl(String Url) {
		String shareContent=String.format(
				this.getResources().getString(R.string.share_content),
				newhouseList.getName(), newhouseList.getLogo());
		// 设置分享内容
		mController
				.setShareContent(shareContent);
		// 设置分享图片, 参数2为图片的地址
		mController.setShareMedia(new UMImage(this,newhouseList.getLogo()));
		// 你在微信开发平台注册应用的App key,
		// 微信图文分享必须设置一个url
		// 添加微信朋友圈平台
		mController.getConfig()
				.supportWXCirclePlatform(this, AppDataConfig.WXapp_id, Url);
		mController.getConfig().supportWXPlatform(this, AppDataConfig.WXapp_id, Url);
	}
	private void init() {
		// 加载图片
		drawableSelected = getResources().getDrawable(
				R.drawable.top_50collection_selected);
		drawableNormal = getResources().getDrawable(
				R.drawable.top_50collection_normal);
		drawableSelected.setBounds(0, 0, drawableSelected.getMinimumWidth(),
				drawableSelected.getMinimumHeight());
		drawableNormal.setBounds(0, 0, drawableNormal.getMinimumWidth(),
				drawableNormal.getMinimumHeight());

		// 显示地图控件
		mapView = (ImageView) this.findViewById(R.id.img_Address_Map);
		tuanView = (ImageView) this.findViewById(R.id.img_tuan);
		mapLayout =(RelativeLayout)this.findViewById(R.id.layout_map_onclick);
		mapLayout.setOnClickListener(this);
		noPicView = (ImageView) this.findViewById(R.id.img_no_house_images);

		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
				.cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build();

		phoneNumEidtText = (EditText) this.findViewById(R.id.et_tuan_phone);
		backBtn = (Button) this.findViewById(R.id.bt_back);
		backBtn.setOnClickListener(this);
		callBtn = (Button) this.findViewById(R.id.bt_call_agent);
		callBtn.setOnClickListener(this);
		getPreBtn = (Button) this.findViewById(R.id.bt_get_tuan);
		getPreBtn.setOnClickListener(this);
		shareBtn = (Button) this.findViewById(R.id.bt_share);
		shareBtn.setOnClickListener(this);
		collectionBtn = (Button) this.findViewById(R.id.bt_collection);
		collectionBtn.setOnClickListener(this);

		tuangouLayout = (LinearLayout) this.findViewById(R.id.layout_tuangou);
		tuanLayoutBtn = (LinearLayout) this.findViewById(R.id.layout_tuan_bt);
		tuanLayoutBtn.setOnClickListener(this);
		text_tuangou_endtime= (TextView) this.findViewById(R.id.text_tuangou_endtime);
		// 如果是团购显示layout
		text_type_tuan= (TextView) this.findViewById(R.id.text_type_tuan);
		switch (attrType) {
		case 1:
			tuanView.setImageResource(R.drawable.tuan);
			text_type_tuan.setText("团购报名中");
			break;
		case 2:
			tuanView.setImageResource(R.drawable.kan);
			text_type_tuan.setText("看房团报名");
			break;
		case 3:
			tuanView.setImageResource(R.drawable.fang);
			break;
		}

		houseBuyView[0] = (TextView) this.findViewById(R.id.text_tuangou_Sales);
		houseBuyView[1] = (TextView) this
				.findViewById(R.id.text_tuangou_SignCount);
		houseBuyView[2] = (TextView) this
				.findViewById(R.id.text_tuangou_RemainTime);
		houseBuyView[3] = (TextView) this
				.findViewById(R.id.text_tuangou_Introduce);

		textViews[0] = (TextView) this.findViewById(R.id.text_Infos_Name);
		textViews[1] = (TextView) this.findViewById(R.id.text_Infos_Status);
		textViews[2] = (TextView) this.findViewById(R.id.text_Infos_Price);
		textViews[3] = (TextView) this.findViewById(R.id.text_Infos_Tel);

		textViews[4] = (TextView) this.findViewById(R.id.text_Position_address);
		textViews[6] = (TextView) this.findViewById(R.id.text_Position_Transit);
		textViews[7] = (TextView) this.findViewById(R.id.text_Position_School);

		textViews[8] = (TextView) this.findViewById(R.id.text_Paras_OpenTime);
		textViews[9] = (TextView) this.findViewById(R.id.text_Paras_BuildType);
		textViews[10] = (TextView) this.findViewById(R.id.text_Paras_Decorate);

		dialog = new MyProgressDialog(this).createDialog(getResources()
				.getString(R.string.loading));
		// 加载进度条
		dialog.show();
		imagesPager = (ViewPager) this.findViewById(R.id.pager_house_images);
		roomTypePager = (ViewPager) this.findViewById(R.id.pager_room_type);
		newsListView = (ListView) this.findViewById(R.id.listview_news);
		newsListView.setOnItemClickListener(this);
		isMoreNewsLayout = (RelativeLayout) this
				.findViewById(R.id.layout_is_more_news);
		isMoreNewsLayout.setOnClickListener(this);
		isNoNewsLayout = (RelativeLayout) this
				.findViewById(R.id.layout_is_no_news);
		moreParasLayout = (RelativeLayout) this
				.findViewById(R.id.layout_more_Paras);
		moreParasLayout.setOnClickListener(this);
		mScrollView = (VerticalScrollView) this.findViewById(R.id.scrollview);
		mScrollView.smoothScrollTo(0, 0);
		new Thread(getNewHouseDetailRun).start();
	}

	private void setTuanData() {
		tuangouLayout.setVisibility(View.VISIBLE);
		if (!"".equals(newhouseBuyInfo.getSales().trim())) {
			houseBuyView[0].setText(newhouseBuyInfo.getSales().trim());
		}
		if (!"".equals(newhouseBuyInfo.getSignCount().trim())) {
			houseBuyView[1].setText(newhouseBuyInfo.getSignCount().trim()
					+ "人已报名");
		}
		if (!"".equals(newhouseBuyInfo.getRemainTime().trim())) {
			houseBuyView[2].setText(newhouseBuyInfo.getRemainTime().trim());
		}
		if (!"".equals(newhouseBuyInfo.getIntroduce().trim())) {
			houseBuyView[3].setText(newhouseBuyInfo.getIntroduce().trim());
		}
		if (!"".equals(newhouseBuyInfo.getRemainTime().trim())) {
			text_tuangou_endtime.setText(newhouseBuyInfo.getRemainTime().trim());
		}
	}

	private void updateUI() {

		if (((NewhouseDetail) newHouseDetailResult.getData().get(0)).getPics()
				.size() == 0) {
			imagesPager.setVisibility(View.GONE);
			noPicView.setVisibility(View.VISIBLE);
		}

		if (isMoreNews) {
			isMoreNewsLayout.setVisibility(View.VISIBLE);
		} else if (isNoNews) {
			isNoNewsLayout.setVisibility(View.VISIBLE);
		}

		imagePagerAdapter = new ImagePagerAdapter(this, imgData,
				R.layout.item_pager_image, new String[] { "img" },
				new int[] { R.id.image });
		imagesPager.setAdapter(imagePagerAdapter);
		imagesPager.setCurrentItem(0);

		housePicPagerAdapter2 = new ImageClickPagerAdapter(this, housePicData,
				R.layout.item_pager_room_type, new String[] { "ID", "Pic",
						"Name", "No", "Size" }, new int[] {
						R.id.un_HousePic_id, R.id.img_HousePic_pic,
						R.id.text_HousePic_name, R.id.text_HousePic_NO,
						R.id.text_HousePic_Size });
		if (housePicData.size() == 0) {
			roomTypePager.setVisibility(View.GONE);
		}

		roomTypePager.setAdapter(housePicPagerAdapter2);
		roomTypePager.setCurrentItem(0);
		newsSimpleAdapter = new SimpleAdapter(this, newsData,
				R.layout.item_list_news, new String[] { "ID", "Title",
						"Content", "PubTime" }, new int[] { R.id.un_News_id,
						R.id.text_News_Title, R.id.text_News_Content,
						R.id.text_News_PubTime });

		newsListView.setAdapter(newsSimpleAdapter);
		setListViewHeightBasedOnChildren(newsListView);

		newhouseDetail = (NewhouseDetail) newHouseDetailResult.getData().get(0);
		if (!"".equals(newhouseDetail.getUrl()))
		changeShareUrl(newhouseDetail.getUrl());
		// 显示百度地图
		String mapUrl = "http://api.map.baidu.com/staticimage?center="
				+ newhouseDetail.getPosition().getLon() + ","
				+ newhouseDetail.getPosition().getLat() + "&width="
				+ MyApplication.widthPixels + "&height="
				+ MyApplication.widthPixels * 0.75
				+ "&zoom=12&markerStyles=-1," + AppDataConfig.baiduMaker;
		imageLoader.displayImage(mapUrl, mapView, options);

		Log.d("mapUrl", mapUrl);
		textViews[0].setText(newhouseDetail.getInfos().getName());
		textViews[1].setText(newhouseDetail.getInfos().getStatus());

		String price = newhouseDetail.getInfos().getPrice() + "";
		if (newhouseDetail.getInfos().getPrice() == 0) {
			price = "售价待定";
		} else {
			price += "元/" + newhouseDetail.getInfos().getPriceUnit();
		}
		textViews[2].setText(price);
		textViews[3].setText(newhouseDetail.getInfos().getTel());
		textViews[4].setText(newhouseDetail.getPosition().getAddress());
		textViews[6].setText(newhouseDetail.getPosition().getTransit());
		textViews[7].setText(newhouseDetail.getPosition().getSchool());
		textViews[8].setText(newhouseDetail.getParas().getOpenTime());
		textViews[9].setText(newhouseDetail.getParas().getBuildType());
		textViews[10].setText(newhouseDetail.getParas().getDecorate());

		// 检测是否已收藏
		if ((collectionChecked = collectionService.getNewHouseById(sHouseId) != null ? true
				: false)) {
			collectionBtn.setCompoundDrawables(drawableSelected, null, null,
					null);
			// collectionBtn.setBackgroundResource(R.drawable.top_50collection_selected);
		}
		mScrollView.smoothScrollTo(0, 0);
	}

	private void getDataError(String ERRORMESSAGE) {
		new AlertDialog.Builder(this).setCancelable(false)
				.setMessage(ERRORMESSAGE)
				.setPositiveButton(getResources().getString

				(R.string.cancel), new DialogInterface.OnClickListener()

				{
					public void onClick

					(DialogInterface dialog, int which) {
						finish();
					}
				}).setNegativeButton(getResources().getString

				(R.string.try_again), new DialogInterface.OnClickListener()

				{
					public void onClick(

					DialogInterface dialogInterface, int

					whichButton) {
						dialog.show();
						new Thread(getNewHouseDetailRun).start();
					}
				}).show();
	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.arg1) {
			case 11:
				updateUI();
				dialog.dismiss();
				break;
			case -11:
				getDataError((String) msg.obj);
				break;
			case -21:
				updateUI();
				dialog.dismiss();
				break;
			case 21:
				updateUI();
				setTuanData();
				dialog.dismiss();
				break;
			case 1:
				if (null!=newhouseBuyInfo)
				tuanService.saveNewhouseBuyInfoJson(newhouseBuyInfo);
				showMessage("恭喜您，报名成功！!");
				break;
			case -1:
				showMessage("抱歉，报名失败，请重试！\n" + (String) msg.obj);
				break;
			}
		}
	};

	// 得到data数据
	private void getData() {
		imgData = new ArrayList<Map<String, Object>>();
		housePicData = new ArrayList<Map<String, Object>>();
		newsData = new ArrayList<Map<String, Object>>();
		NewhouseDetail newhouseDetail;
		Map<String, Object> map;
		if (newHouseDetailResult.getSuccess()) {
			Log.e("size", newHouseDetailResult.getData().size() + "");
			newhouseDetail = (NewhouseDetail) newHouseDetailResult.getData()
					.get(0);
			Map<String, String> pics = newhouseDetail.getPics();

			if (pics == null || pics.size() == 0) {
				map = new HashMap<String, Object>();
				map.put("img", R.drawable.ic_empty);
				imgData.add(map);
			}
			for (int i = 0; i < pics.size(); i++) {
				map = new HashMap<String, Object>();
				map.put("img", pics.get("Pics" + (i + 1)));
				imgData.add(map);
			}

			List<NewhouseDetailHousePic> housePicList = newhouseDetail
					.getHousePicList();
			NewhouseDetailHousePic housePic;
			if (housePicList != null) {
				for (int i = 0; i < housePicList.size(); i++) {
					map = new HashMap<String, Object>();
					housePic = housePicList.get(i);
					map.put("ID", housePic.getId() + "");
					map.put("Pic", housePic.getPic().trim());
					map.put("Name", housePic.getName().trim());
					map.put("No", housePic.getNo().trim());
					map.put("Size", housePic.getSize() + "平米");
					map.put("Direction", housePic.getDirection().trim());
					housePicData.add(map);
				}
			}

			List<NewhouseNewsDetail> newsList = newhouseDetail.getNewsList();
			NewhouseNewsDetail news;
			if (newsList != null) {
				int num = newsList.size();
				if (num > 3) {
					isMoreNews = true;
					num = 3;
				} else if (num == 0) {
					isNoNews = true;
				}
				for (int i = 0; i < num; i++) {
					map = new HashMap<String, Object>();
					news = newsList.get(i);
					map.put("ID", news.getId() + "");
					String title = news.getTitle();
					if (title.length() > 16) {
						title = title.substring(0, 16);
						title += "…";
					}
					map.put("Title", title);
					map.put("Content", news.getContent().trim());
					map.put("PubTime", news.getPubTime());
					newsData.add(map);
				}
			} else {
				isNoNews = true;
			}

		}

	}

	public void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = 1 + totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
		listView.setLayoutParams(params);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_share:
			// 打开平台选择面板，参数2为打开分享面板时是否强制登录,false为不强制登录
			mController.openShare(this, false);
			break;
		case R.id.bt_collection:
			// 收藏与取消
			if (collectionChecked) {
				// sharedPreferences.removeMapItemMessage(
				// AppDataConfig.CollectionKeys[1], newhouseList.getId() + "");
				collectionService.deleteNewById(sHouseId);
				collectionBtn.setCompoundDrawables(drawableNormal, null, null,
						null);
				collectionChecked = false;
				Toast.makeText(this, "取消收藏！", Toast.LENGTH_SHORT).show();
			} else {
				// sharedPreferences.saveMapItemMessage(
				// AppDataConfig.CollectionKeys[1], newhouseList.getId() + "",
				// newhouseList);
				collectionService.saveNewHouse(newhouseList);
				collectionBtn.setCompoundDrawables(drawableSelected, null,
						null, null);
				Toast.makeText(this, "收藏成功！", Toast.LENGTH_SHORT).show();
				collectionChecked = true;
			}
			break;
		case R.id.layout_is_more_news:
			Intent intent = new Intent(this, MoreNewsActivity.class);
			intent.putExtra("houseId", sHouseId);
			startActivity(intent);
			break;
		case R.id.layout_more_Paras:
			Intent intent2 = new Intent(this, MoreParasActivity.class);
			intent2.putExtra("houseId", sHouseId);
			intent2.putExtra("Address", newhouseDetail.getPosition().getAddress());
			intent2.putExtra("School", newhouseDetail.getPosition().getSchool());
			startActivity(intent2);
			break;

		case R.id.bt_back:
			this.finish();
			break;
		case R.id.bt_call_agent:
			String tel = newhouseDetail.getInfos().getTel();
			if (tel == null || "".equals(tel.trim())) {
				Toast.makeText(NewHouseDetailActivity.this, "抱歉，该楼盘售楼处没有提供电话！",
						Toast.LENGTH_SHORT).show();
			} else {
				tel=ValidityTools.checkFenjihao(tel);
				Intent phoneIntent = new Intent("android.intent.action.CALL",
						Uri.parse("tel:" + tel));
				startActivity(phoneIntent);
				callService.saveNewHouse(newhouseList);
			}

			break;
		case R.id.layout_tuan_bt:
			// 跳转到团购详细页面
//			Intent intent3 = new Intent(this, TuangouDetailActivity.class);
//			intent3.putExtra("houseId", sHouseId);
//			intent3.putExtra("newhouseBuyInfo", newhouseBuyInfo);
//			startActivity(intent3);
			break;
		case R.id.bt_get_tuan:
			// 检测手机号合法性
//			tuanPhone = phoneNumEidtText.getText().toString();
//			if (ValidityTools.checkPhoneNum(tuanPhone)) {
//				// 发送团购请求
//				new Thread(getNewhouseBuySignRun).start();
//			} else {
//				Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
//			}
			Intent intent3 = new Intent(this, TuangouDetailActivity.class);
			intent3.putExtra("houseId", sHouseId);
			intent3.putExtra("newhouseBuyInfo", newhouseBuyInfo);
			startActivity(intent3);
			break;
		case R.id.layout_map_onclick:
			Intent mapIntent=new Intent(NewHouseDetailActivity.this, MapRoutePlanActivity.class);
			ConvertSwitch.Convert_GCJ02_To_BD09(newhouseDetail.getPosition().getLat(), newhouseDetail.getPosition().getLon());
			MapRoutePlanActivity.houseName=newhouseList.getName();
			mapIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			startActivity(mapIntent);
			break;
		default:
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		TextView idText = (TextView) arg1.findViewById(R.id.un_News_id);
		TextView pubTime = (TextView) arg1.findViewById(R.id.text_News_PubTime);
		Intent intent = new Intent(this, HouseNewsDetailActivity.class);
		Log.e("houseNewsId", idText.getText() + "");
		intent.putExtra("houseNewsId", idText.getText() + "");
		intent.putExtra("pubTime", pubTime.getText());
		startActivity(intent);

	}

	// 自定义可点击的图片的适配器
	class ImageClickPagerAdapter extends ImagePagerAdapter {

		public ImageClickPagerAdapter(Context context,
				List<Map<String, Object>> data, int layoutId, String[] sNmaes,
				int[] ids) {
			super(context, data, layoutId, sNmaes, ids);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Object instantiateItem(ViewGroup view, int position) {
			// TODO Auto-generated method stub
			super.instantiateItem(view, position);
			ImageView imageView = (ImageView) imageLayout
					.findViewById(R.id.img_HousePic_pic);
			imageView.setOnClickListener(new MyOnclickListener(sHouseId,
					position));
			return imageLayout;
		}

		class MyOnclickListener implements OnClickListener {

			String houseId, typeId;
			int position;

			public MyOnclickListener(String houseId, int position) {
				this.position = position;
				this.houseId = houseId;
			}

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(NewHouseDetailActivity.this,
						NewHouseTypeListActivity.class);
				intent.putExtra("houseId", sHouseId);
				intent.putExtra("position", position);
				startActivity(intent);
			}

		}

	}

	/**
	 * 线程 得到新房列表
	 */
	Runnable getNewHouseDetailRun = new Runnable() {

		@Override
		public void run() {
			Message msg = handler.obtainMessage();
			String jsonString;
			String url = AppDataConfig.xinfangUrl
					+ AppDataConfig.NewhouseDetail + "?id=" + sHouseId;
			Log.e("url", url);
			jsonString = HttpTools.getJsonContent(url);
			Log.e("JsonString", jsonString);
			newHouseDetailResult = JsonTools.getNewhouseDetail(jsonString);
			getData();

			if(newHouseDetailResult.getSuccess()){
				if (attrType != 0) {
					String jsonStringTuan;
					String urlTuan = AppDataConfig.xinfangUrl
							+ AppDataConfig.NewhouseBuyInfo + "?houseid="
							+ sHouseId;
					Log.e("url", urlTuan);
					jsonStringTuan = HttpTools.getJsonContent(urlTuan);
					Log.e("jsonStringTuan", jsonStringTuan);
					newHouseBuyInfoResult = JsonTools
							.getNewhouseBuyInfo(jsonStringTuan);

					if (newHouseBuyInfoResult.getSuccess()) {
						if (newHouseBuyInfoResult.getData().size() > 0) {
							newhouseBuyInfo = (NewhouseBuyInfo) newHouseBuyInfoResult
									.getData().get(0);
							newhouseBuyInfo.setHouseId(sHouseId);
							msg.arg1 = 21;
						} else {
							msg.arg1 = -21;
							//数据为空
							String err = newHouseBuyInfoResult.getErrorMessage();
							msg.obj = err.trim();
						}
					}else {
						//请求失败
						msg.arg1 = -21;
						String err = newHouseBuyInfoResult.getErrorMessage();
						msg.obj = err.trim();
					}
				} else{
					if (newHouseDetailResult.getSuccess()) {
						msg.arg1 = 11;
					}
				}
			}else {
				msg.arg1 = -11;
				msg.obj = newHouseDetailResult.getErrorMessage();
			}
			
			handler.sendMessage(msg);
		}
	};

	/**
	 * 线程 得到团购信息
	 */
	Runnable getNewhouseBuySignRun = new Runnable() {

		@Override
		public void run() {
			Message msg = handler.obtainMessage();
			String url = AppDataConfig.xinfangUrl
					+ AppDataConfig.NewhouseBuySign + "?houseid=" + sHouseId
					+ "&mobile=" + tuanPhone;
			Log.e("url", url);
			tuanJsonString = HttpTools.getJsonContent(url);
			Log.e("JsonString", tuanJsonString);
			newhouseBuySignResult = JsonTools
					.getNewhouseBuySignResult(tuanJsonString);
			if (newhouseBuySignResult.getSuccess()) {
				msg.arg1 = 1;
			} else {
				msg.arg1 = -1;
				msg.obj = newhouseBuySignResult.getErrorMessage();
			}
			handler.sendMessage(msg);
		}
	};

	protected void showMessage(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
}
